import tkinter as tk
from tkinter import ttk

from main_logic import Bin2Dec, Dec2Bin

def config():
    CONFIG = {
        "window_width": 450,
        "window_height": 140,

        "blue_color": "#6f9bc6",
        "yellow_color": "#f2993a",
    }
    return CONFIG


class Application(tk.Frame):

    def __init__(self, curr_tk=None):
        super().__init__(curr_tk)

        self.curr_tk = curr_tk
        self.curr_tk.title("Binary to Decimal or Decimal to Binary")

        screen_width = self.curr_tk.winfo_screenwidth() # 屏幕的宽
        screen_height = self.curr_tk.winfo_screenheight() # 屏幕的高
        tk_width, tk_height = CONFIG.get("window_width"), CONFIG.get("window_height")
        tk_x = (screen_width - tk_width) // 2
        tk_y = (screen_height - tk_height) // 2
        self.curr_tk.geometry(f"{tk_width}x{tk_height}+{tk_x}+{tk_y}")
        self.curr_tk.config(bg="skyblue")

        frame = tk.Frame(self.curr_tk, width=500, height=200, bg=CONFIG.get("blue_color"))
        frame.grid(row=0, column=0, padx=10, pady=5)

        label = ttk.Label(frame, text='Enter tried convert number:', background=CONFIG.get("blue_color"))
        label.grid(row=0, column=0, padx=5, pady=5)
        button1 = ttk.Button(frame, text='Bin to Dec', command=self.main_bin2dec_logic)
        button1.grid(row=1, column=0, padx=5, pady=5)
        button2 = ttk.Button(frame, text='Dec to Bin', command=self.main_dec2bin_logic)
        button2.grid(row=1, column=1, padx=5, pady=5)

        self.input_str = tk.StringVar()
        self.result_str = tk.StringVar()

        result = ttk.Label(frame, textvariable=self.result_str)
        result.grid(row=2, column=1, padx=5, pady=5)
        get_input = ttk.Entry(frame, textvariable=self.input_str)
        get_input.grid(row=0, column=1, padx=5, pady=5)

    def run_tk(self):
        self.curr_tk.mainloop()

    def main_bin2dec_logic(self):
        try:
            input_str = str(self.input_str.get())
            bin2dec_ans = Bin2Dec().bin_to_dec(input_str)
            self.result_str.set(f"Answer: {bin2dec_ans}")
        except Exception as exce:
            self.result_str.set("Invalid input")
    
    def main_dec2bin_logic(self, **kwargs):
        try:
            input_str = str(self.input_str.get())
            dec2bin_ans = Dec2Bin().dec_to_bin(input_str)
            self.result_str.set(f"Answer: {dec2bin_ans}")
        except Exception as exce:
            self.result_str.set("Invalid input")



def start():
    new_tk = tk.Tk()
    my_tk = Application(curr_tk=new_tk)
    my_tk.run_tk()


CONFIG = config()
start()
